package com.photoeditor.demo.ui.widget.image.edit;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import com.common.base.utils.DeviceUtils;
import com.photoeditor.R;
import com.photoeditor.demo.model.bean.BitmapBean;
import com.photoeditor.demo.model.image.collage.templet.CollageTemplet;
import com.photoeditor.demo.model.image.collage.templet.Ratio;
import com.photoeditor.demo.model.image.util.ImageHelper;
import com.photoeditor.demo.ui.widget.collage.CollageBackgroundView;
import com.photoeditor.demo.ui.widget.collage.CollageRelativeLayout;
import com.photoeditor.demo.util.AsyncTask;
import com.photoeditor.demo.util.ImageManageUtil;

import java.util.ArrayList;

public class MirrorListAdapter extends ArrayAdapter<CollageTemplet> {

    private ArrayList<CollageTemplet> mTemplates;

    private Ratio.RATIO mType;

    private LayoutInflater mInfl;

    private int mSelectedPosition = 0;

    private final int mLeftPadding;
    private final int mRightPadding;
    private final int mLastRightPadding;

    private final int mWidth;
    private final int mHeight;

    private Bitmap mBaseBitmap;
    private BitmapBean mBitmapBean;

    public MirrorListAdapter(Context context, ArrayList<CollageTemplet> data, Ratio.RATIO type) {
        super(context, 0, data);
        mTemplates = data;
        mType = type;
        mInfl = ((Activity) context).getLayoutInflater();
        mLeftPadding = DeviceUtils.dip2px(getContext(), 6);
        mRightPadding = DeviceUtils.dip2px(getContext(), 7);
        mLastRightPadding = DeviceUtils.dip2px(getContext(), 13);
        mWidth = getContext().getResources().getDimensionPixelSize(R.dimen.crop_height);
        mHeight = mWidth;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        CollageTemplet templet = mTemplates.get(position);
        if (templet.isType(CollageTemplet.TYPE_POINT)) {
            if (convertView == null) {
                convertView = mInfl.inflate(R.layout.item_collage_mirror, null);
                CollageRelativeLayout templetView = (CollageRelativeLayout) convertView.findViewById(R.id.collage_mirror);
                CollageBackgroundView selectView = (CollageBackgroundView) convertView.findViewById(R.id.collage_select_layout);
                holder = new ViewHolder();
                holder.mirrorView = templetView;
                holder.selectView = selectView;
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.mirrorView.setSourceBitmaps(mBaseBitmap);
            ArrayList<BitmapBean> list = new ArrayList<>(1);
            list.add(mBitmapBean);
            holder.mirrorView.setSourceBitmapBeans(list);
            holder.mirrorView.setMode(CollageRelativeLayout.MODE_MIRROR);
            holder.mirrorView.setShareOperation(true);
            holder.mirrorView.setList(true);
            holder.mirrorView.setType(mType);
            holder.mirrorView.setTemplet(templet);
            if (!holder.mirrorView.isInit()) {
                holder.mirrorView.invalidateListCollage(mWidth, mHeight);
            }
            if (mSelectedPosition == position) {
                holder.selectView.setShow(true);
                holder.selectView.setColor(getContext().getResources().getColor(R.color.accent_color));
            } else {
                holder.selectView.setShow(false);
            }
        } else if (templet.isType(CollageTemplet.TYPE_SVG)) {
            //show svg View
        }
        //处理UI细节
        if (position == getCount() - 1) {
            convertView.setPadding(mLeftPadding, 0, mLastRightPadding, 0);
        } else {
            convertView.setPadding(mLeftPadding, 0, mRightPadding, 0);
        }
        return convertView;
    }

    @Override
    public CollageTemplet getItem(int position) {
        if (mTemplates != null) {
            return mTemplates.get(position);
        }
        return null;
    }

    public void setData(ArrayList<CollageTemplet> data) {
        mTemplates = data;
        notifyDataSetChanged();
    }

    public void setType(Ratio.RATIO type) {
        mType = type;
        notifyDataSetChanged();
    }

    /**
     * @param position
     * @param convertView
     */
    public void setSelectedPosition(int position, View convertView) {
        mSelectedPosition = position;
        CollageBackgroundView backgroundView = (CollageBackgroundView) convertView.findViewById(R.id.collage_select_layout);
        backgroundView.setShow(true);
        backgroundView.setColor(getContext().getResources().getColor(R.color.accent_color));
        ViewGroup vp = (ViewGroup) convertView.getParent();
        int child = vp.getChildCount();
        for (int i = 0; i < child; i++) {
            View v = vp.getChildAt(i);
            if (v != convertView) {
                CollageBackgroundView bv = (CollageBackgroundView) v.findViewById(R.id.collage_select_layout);
                bv.setShow(false);
            }
        }
    }

    public void setSelectedPosition(int position) {
        mSelectedPosition = position;
        notifyDataSetChanged();
    }

    public void setBaseBitmap(final Bitmap sourceBitmap, final BitmapBean bitmapBean) {
        mBitmapBean = bitmapBean;
        new AsyncTask<Void, Void, Bitmap>() {

            @Override
            protected Bitmap doInBackground(Void... params) {
                return ImageHelper.getThumbnail(sourceBitmap, 0, ImageManageUtil.getSmallImageCacheWidth(), ImageManageUtil.getSmallImageCacheWidth(), false);
            }

            protected void onPostExecute(Bitmap result) {
                super.onPostExecute(result);
                mBaseBitmap = result;
                if (mBaseBitmap != null) {
                    notifyDataSetChanged();
                }
            }

            ;
        }.executeOnExecutor(AsyncTask.DUAL_THREAD_EXECUTOR);
    }

    public class ViewHolder {
        CollageRelativeLayout mirrorView;
        CollageBackgroundView selectView;
    }
}
